package com.qf.service;

import com.qf.dao.UserMapper;
import com.qf.pojo.Student;
import com.qf.pojo.User;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * @Service: 标记：UserServiceImpl是需要通过IOC进入到Spring容器
 *    <bean id = "userService"  class="com.qf.service.UserServiceImpl" scope="单例"/>
 * @author lixu
 */
//@Service(value = "uS")
@Service //默认ID就是 userService
//@Scope("prototype")//当前UserServiceImpl就是多例的
//@Scope("singleton")//当前UserServiceImpl就是单例的
@Transactional //事务注解  类头上：表示整个类都添加上注解了 不是好习惯 懒
public class UserServiceImpl implements UserService{


    /**
     * //注解式依赖注入
     *   1、通过类型注入 成功了
     *   2、通过ID注入
     */
    @Autowired
    //@Qualifier("userMapper")
    private UserMapper uM;


    /**
     *   start tranaction
     *   select * from user
     *   commit
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<User> findUser() {
        return uM.findUser();
    }

    /**
     * 添加
     * @Transactional(readOnly = false,propagation = 隔离级别)
     * timeout = -1: 单位是s
     *
     *  rollbackFor= IOException.class,rollbackForClassName = "java.io.IOException"
     *   表示：在RuntimeException及其子异常基础之上 扩展IO流异常
     *   noRollbackFor=ArithmeticException.class,noRollbackForClassName = "java.io.ArithmeticException"
     *   表示：在RuntimeException及其子异常基础之上 减少算术异常
     * @param user
     */
    @Override
    @Transactional(readOnly = false,isolation = Isolation.REPEATABLE_READ,
            propagation = Propagation.REQUIRED,timeout = -1,
            rollbackFor= {IOException.class, NotFoundException.class},rollbackForClassName = "java.io.IOException",
            noRollbackFor=ArithmeticException.class,noRollbackForClassName = "java.io.ArithmeticException"
    )
    public void addUser(User user, Student student) {
        uM.add(user);
        uM.update(student);
    }



    @Transactional //事务注解
    public void updateUser(){


    }
    @Transactional //事务注解
    public void deleteUser(){



    }


    /**
     * Aop切面 注解式开发
     */
    @Override
    public Object zufang() {
        //核心代码
        System.out.println("3:签合同");
        //
        //int i = 1/0;
        System.out.println("4:收房租");
        return "我是房东，我怕谁啊";
    }
}
